<template>
  <el-dialog
    :title="title"
    width="40vw"
       :close-on-click-modal="false"
    :visible.sync="dialogVisible"
    @close="handleClose"
    append-to-body
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="供应标题" prop="supplyTitle">
        <el-input v-model="form.supplyTitle" placeholder="请输入供应标题" />
      </el-form-item>
      <el-form-item label="供应描述" prop="supplyDesc">
        <el-input
          v-model="form.supplyDesc"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
      <el-form-item label="城市名" prop="cityName">
        <el-select v-model="form.cityName" placeholder="请选择城市名" clearable>
          <el-option
            v-for="dict in dict.type.city_names"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="租出算力能力(PFLOPS)" prop="gpuApplyPflops">
        <el-input
          v-model="form.gpuApplyPflops"
          placeholder="请输入租出算力能力(PFLOPS)"
        />
      </el-form-item>
      <el-form-item label="计算资源" prop="gpuResource">
        <el-select
          v-model="form.gpuResource"
          placeholder="请选择算力-计算资源"
          clearable
        >
          <el-option
            v-for="dict in dict.type.gpu_resource_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="网络架构" prop="gpuNet">
        <el-input v-model="form.gpuNet" placeholder="请输入网络架构" />
      </el-form-item>
      <el-form-item label="计费模式" prop="gpuAbilityType">
        <el-select
          v-model="form.gpuAbilityType"
          placeholder="请选择计费模式"
          clearable
        >
          <el-option label="算力包月" :value="1" />
          <el-option label="卡时计费" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="可租数量" prop="gpuNum">
        <el-input v-model="form.gpuNum" placeholder="请输入可租数量" />
      </el-form-item>
      <el-form-item label="单价" prop="gpuPrice">
        <el-input v-model="form.gpuPrice" placeholder="请输入单价" />
      </el-form-item>
      <el-form-item label="卡类型" prop="gpuType">
        <el-select v-model="form.gpuType" placeholder="请选择卡类型" clearable>
          <el-option
            v-for="dict in dict.type.supply_gpu_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="适用场景" prop="gpuOccasion">
        <el-input v-model="form.gpuOccasion" placeholder="请输入适用场景" />
      </el-form-item>
      <el-form-item label="可租时间-起" prop="supplyBegin">
        <el-date-picker
          clearable
          v-model="form.supplyBegin"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择可租时间-起"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="可租时间-止" prop="supplyEnd">
        <el-date-picker
          clearable
          v-model="form.supplyEnd"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择可租时间-止"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="上架截止时间" prop="deadTime">
        <el-date-picker
          clearable
          v-model="form.deadTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择上架截止时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入内容"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="loading"
        >确 定</el-button
      >
      <el-button @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addSupply, updateSupply } from "@/api/system/supply";
export default {
  name: "SuanliPopup",
  dicts: [
    "operator",
    "power_way",
    "route_type",
    "gpu_resource_type",
    "city_names",
    "supply_gpu_type",
  ],
  props: {
    visible: Boolean,
    formData: [Object, null],
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      loading: false,
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit("update:visible", val);
      },
    },
    title() {
      if (this.formData && this.formData.id) return "修改供应信息";
      return "添加供应信息";
    },
  },
  watch: {
    dialogVisible(val) {
      if (val && this.formData) {
        this.form = structuredClone(this.formData);
      }
    },
  },
  methods: {
    handleClose() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplyTitle: null,
        supplyDesc: null,
        supplyType: null,
        cityName: null,
        framePowerWay: null,
        framePower: null,
        frameHeight: null,
        framePrice: null,
        supplyNum: null,
        supplyBegin: null,
        supplyEnd: null,
        deadTime: null,
        auditStatus: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        bwOperator: null,
        bwRouteType: null,
        bwBandwidthPrice: null,
        bwBandwidthNum: null,
        bwIpPrice: null,
        bwIpNum: null,
        gpuAbilityType: null,
        gpuApplyPflops: null,
        gpuResource: null,
        gpuNet: null,
        gpuNum: null,
        gpuOccasion: null,
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.id != null) {
            updateSupply(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
              })
              .finally(() => {
                this.loading = false;
                this.dialogVisible = false;
              });
          } else {
            addSupply(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
              })
              .finally(() => {
                this.loading = false;
                this.dialogVisible = false;
              });
          }
        }
      });
    },
  },
};
</script>
